class Bill:
    def __init__(self, type, account, money, time, comment):
        self.type = type
        self.account = account
        self.money = money
        self.time = time
        self.comment = comment
    
    def __str__(self):
        return f'{self.type}\t\t\t\t\t{self.account}\t\t\t\t\t{self.money}\t\t\t\t\t{self.time}\t\t\t{self.comment}'
bills = []

def userview():
    while True:
        option = input('1.查看所有 2.按条件查询 3.退出系统：')
        if option == '1':
            print_head()
            for i, bill in enumerate(bills):
                print(f'{i+1}\t\t\t\t\t{bill}')
        elif option == '2':
            searchoption()
        elif option == '3':
            print('退出系统')
            break
        else:
            print('输入错误，请重新输入')

def searchoption():
    while True:
        searchopt = input('1.按编号 2.按类别 3.按账户 4.按金额 5.按时间 6.按说明 7.退出查询：')
        if searchopt == '1':
            try:
                searchid = int(input('请输入账单编号：'))
                if searchid <= 0 or searchid > len(bills):
                    print('没有这条数据')
                    continue
                print_head()
                print(f'{searchid}\t\t\t\t\t{bills[searchid-1]}')
            except ValueError:
                print('输入有误')
        elif searchopt == '2':
            searchtype = input('请输入账单类型：')
            print_head()
            for i, bill in enumerate(bills):
                if searchtype in bill.type:
                    print(f'{i+1}\t\t\t\t\t\t{bill}')
        elif searchopt == '3':
            searchaccount = input('请输入账单账户：')
            print_head()
            for i, bill in enumerate(bills):
                if searchaccount in bill.account:
                    print(f'{i+1}\t\t\t\t\t{bill}')
        elif searchopt == '4':
            try:
                minmoney = float(input('请输入最小金额：'))
                maxmoney = float(input('请输入最大金额：'))
                print_head()
                for i, bill in enumerate(bills):
                    if minmoney <= float(bill.money) <= maxmoney:
                        print(f'{i + 1}\t\t\t\t\t{bill}')
            except ValueError:
                print('输入金额格式不正确喵')
        elif searchopt == '5':
            mintime = input('请输入最小时间(xxxx-xx-xx)：')
            maxtime = input('请输入最大时间(xxxx-xx-xx)：')
            print_head()
            for i, bill in enumerate(bills):
                if mintime <= bill.time <= maxtime:
                    print(f'{i + 1}\t\t\t\t\t{bill}')
        elif searchopt == '6':
            searchcomment = input('请输入账单说明：')
            print_head()
            for i, bill in enumerate(bills):
                if searchcomment in bill.comment:
                    print(f'{i + 1}\t\t\t\t\t{bill}')
        elif searchopt == '7':
            break
        else:
            print('输入有误，请重新输入')


def print_head():
    print('ID\t\t\t\t\t类别\t\t\t\t\t账户\t\t\t\t\t金额\t\t\t\t\t时间\t\t\t\t\t说明')


def adminview():
    while True:
        option = input('1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统：')
        if option == '1':
            bil_type = input('请输入账单类型：')
            bil_account = input('请输入账户：')
            bil_money = input("请输入金额：")
            try:
                bil_money = float(bil_money)
            except ValueError:
                print('输入的金额不是合法数字')
                continue
            bil_time = input("请输入时间(格式为xxxx-xx-xx)：")
            if len(bil_time) != 10 or bil_time[4] != '-' or bil_time[7] != '-':
                print('时间格式不正确，请重新输入')
                continue
            bil_comment = input('请输入账单说明：')
            newbill = Bill(bil_type, bil_account, bil_money, bil_time, bil_comment)
            bills.append(newbill)
            print(newbill)
        elif option == '2':
            try:
                editnum = int(input('你要编辑哪条数据（编号）：')) - 1
                if editnum < 0 or editnum >= len(bills):
                    print('没有这条数据')
                    continue
                editoption = input('你要修改哪个数据(1:类型 2:账户 3:金额 4:时间 5:说明)：')
                if editoption == '1':
                    bills[editnum].type = input('修改后的类型：')
                elif editoption == '2':
                    bills[editnum].account = input('修改后的账户：')
                elif editoption == '3':
                    try:
                        bills[editnum].money = float(input('修改后的金额：'))
                    except ValueError:
                        print('金额格式错误')
                elif editoption == '4':
                    bills[editnum].time = input('修改后的时间：')
                elif editoption == '5':
                    bills[editnum].comment = input('修改后的说明：')
                else:
                    print('输入有误，请重新输入')
            except ValueError:
                print('输入有误，请重新输入')
        elif option == '3':
            try:
                deletenum = int(input('你要删除哪条数据（编号）：')) - 1
                if deletenum < 0 or deletenum >= len(bills):
                    print('没有这条数据')
                    continue
                del bills[deletenum]
                print('删除成功')
            except ValueError:
                print('输入有误，请重新输入')
        elif option == '4':
            searchoption()
        elif option == '5':
            print('退出系统')
            break
        else:
            print('输入有误，请重新输入')

def homeaccount():
    while True:
        option = input('登陆(login)还是退出(quit)：')
        if option == 'login':
            print('欢迎使用家庭账户管理系统~输入账号密码来登录（三次错误自动退出）')
            for i in range(3):
                account = input('账号：')
                password = input('密码：')
                if account == 'admin' and password == '123456':
                    adminview()
                    return
                elif account == 'lisi' and password == '123456':
                    userview()
                    return
                else:
                    print(f'账号或密码错误，还剩 {2 - i} 次机会')
            print('输错三次，拜拜喽喵~')
            return
        elif option == 'quit':
            print('欢迎下次再来~')
            return
        else:
            print('输入有误，请重新输入')

# 启动程序！
homeaccount()
